package com.luhuiguo.cms.client.ui;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.client.presenter.EditArticlePresenter;
import com.luhuiguo.cms.client.widget.LabelValueComboBox;
import com.luhuiguo.cms.shared.model.ArticleModel;

@Singleton
public class EditArticleView extends ContentPanel implements EditArticlePresenter.Display {

    private GlobalConstants constants;

    private GlobalMessages messages;

    private FormPanel formPanel;

    private FormBinding formBinding;

    private TextField<String> titleField;

    private TextField<String> subjectField;

    private TextField<String> urlField;

    private TextField<String> authorField;

    private TextField<String> editorField;

    private TextField<String> sourceField;

    private TextField<String> keywordsField;

    private TextField<String> descriptionField;

    private TextArea summaryField;

    private NumberField rankField;

    private NumberField gradeField;

    private DateField publishDateField;

    private DateField expiredDateField;

    private LabelValueComboBox<Integer> statusField;

    private LabelValueComboBox<Boolean> allowCommentField;

    private NumberField clicksField;

    private NumberField buzzUpField;

    private NumberField buzzDownField;

    private HtmlEditor contentField;

    private Button submitButton;

    private Button cancelButton;

    @SuppressWarnings("unused")
    private AttachmentsView attachmentsView;

    @Inject
    public EditArticleView(GlobalConstants constants, GlobalMessages messages, AttachmentsView attachmentsView) {
        this.constants = constants;
        this.messages = messages;

        this.attachmentsView = attachmentsView;

        this.setLayout(new BorderLayout());

        BorderLayoutData data = new BorderLayoutData(LayoutRegion.EAST, 200);
        data.setMargins(new Margins(0, 0, 0, 5));
        data.setSplit(true);
        data.setCollapsible(true);
        this.add(attachmentsView, data);

        data = new BorderLayoutData(LayoutRegion.CENTER);
        data.setMargins(new Margins());

        createFormPanel();

        this.add(formPanel, data);

        this.setBorders(false);
        this.setBodyBorder(false);

        this.setHeaderVisible(false);

    }

    private FieldSet createInfoFieldSet() {

        FieldSet fieldSet = new FieldSet();
        fieldSet.setHeading(messages.articleInfo());
        fieldSet.setCollapsible(true);
        fieldSet.setExpanded(false);
        fieldSet.setLayout(new ColumnLayout());

        LayoutContainer left = new LayoutContainer();
        left.setStyleAttribute("paddingRight", "10px");
        FormLayout layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        left.setLayout(layout);

        LayoutContainer right = new LayoutContainer();
        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        right.setLayout(layout);

        fieldSet.add(left, new ColumnData(.5));
        fieldSet.add(right, new ColumnData(.5));

        urlField = new TextField<String>();
        urlField.setName("url");
        urlField.setFieldLabel(messages.url());
        urlField.setMaxLength(constants.maxStringLength());

        subjectField = new TextField<String>();
        subjectField.setName("subject");
        subjectField.setFieldLabel(messages.subject());
        subjectField.setMaxLength(constants.maxStringLength());

        keywordsField = new TextField<String>();
        keywordsField.setName("keywords");
        keywordsField.setFieldLabel(messages.keywords());
        keywordsField.setMaxLength(constants.maxStringLength());

        descriptionField = new TextField<String>();
        descriptionField.setName("description");
        descriptionField.setFieldLabel(messages.description());
        descriptionField.setMaxLength(constants.maxStringLength());

        FormData formData = new FormData("100%");
        left.add(urlField, formData);
        right.add(subjectField, formData);
        left.add(keywordsField, formData);
        right.add(descriptionField, formData);
        return fieldSet;

    }

    private FieldSet createPropertiesFieldSet() {

        FieldSet fieldSet = new FieldSet();
        fieldSet.setHeading(messages.articleProperties());
        fieldSet.setCollapsible(true);
        fieldSet.setExpanded(false);
        fieldSet.setLayout(new ColumnLayout());

        authorField = new TextField<String>();
        authorField.setName("author");
        authorField.setFieldLabel(messages.author());
        authorField.setMaxLength(constants.maxStringLength());

        editorField = new TextField<String>();
        editorField.setName("editor");
        editorField.setFieldLabel(messages.editor());
        editorField.setMaxLength(constants.maxStringLength());

        sourceField = new TextField<String>();
        sourceField.setName("source");
        sourceField.setFieldLabel(messages.source());
        sourceField.setMaxLength(constants.maxStringLength());

        statusField = new LabelValueComboBox<Integer>();
        statusField.setName("status");
        statusField.setFieldLabel(messages.status());
        statusField.add(messages.abandoned(), ArticleModel.ABANDONED);
        statusField.add(messages.draft(), ArticleModel.DRAFT);
        statusField.add(messages.pending(), ArticleModel.PENDING);
        statusField.add(messages.approved(), ArticleModel.APPROVED);
        statusField.add(messages.published(), ArticleModel.PUBLISHED);
        statusField.setAllowBlank(false);
        statusField.setEditable(false);
        statusField.setTriggerAction(TriggerAction.ALL);

        rankField = new NumberField();
        rankField.setPropertyEditorType(Integer.class);
        rankField.setAllowDecimals(false);
        rankField.setName("rank");
        rankField.setFieldLabel(messages.rank());

        gradeField = new NumberField();
        gradeField.setPropertyEditorType(Integer.class);
        gradeField.setAllowDecimals(false);
        gradeField.setName("grade");
        gradeField.setFieldLabel(messages.grade());

        publishDateField = new DateField();
        publishDateField.setName("publishDate");
        publishDateField.setFieldLabel(messages.publishDate());
        publishDateField.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM-dd"));

        expiredDateField = new DateField();
        expiredDateField.setName("expiredDate");
        expiredDateField.setFieldLabel(messages.expiredDate());
        expiredDateField.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM-dd"));

        allowCommentField = new LabelValueComboBox<Boolean>();
        allowCommentField.setName("allowComment");
        allowCommentField.setFieldLabel(messages.allowCommint());
        allowCommentField.add(messages.yes(), Boolean.TRUE);
        allowCommentField.add(messages.no(), Boolean.FALSE);
        allowCommentField.setEditable(false);
        allowCommentField.setTriggerAction(TriggerAction.ALL);

        clicksField = new NumberField();
        clicksField.setPropertyEditorType(Integer.class);
        clicksField.setAllowDecimals(false);
        clicksField.setName("clicks");
        clicksField.setFieldLabel(messages.clicks());

        buzzUpField = new NumberField();
        buzzUpField.setPropertyEditorType(Integer.class);
        buzzUpField.setAllowDecimals(false);
        buzzUpField.setName("buzzUp");
        buzzUpField.setFieldLabel(messages.buzzUp());

        buzzDownField = new NumberField();
        buzzDownField.setPropertyEditorType(Integer.class);
        buzzDownField.setAllowDecimals(false);
        buzzDownField.setName("buzzDown");
        buzzDownField.setFieldLabel(messages.buzzDown());

        LayoutContainer col1 = new LayoutContainer();
        col1.setStyleAttribute("paddingRight", "10px");
        FormLayout layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        col1.setLayout(layout);

        LayoutContainer col2 = new LayoutContainer();
        col2.setStyleAttribute("paddingRight", "10px");
        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        col2.setLayout(layout);

        LayoutContainer col3 = new LayoutContainer();
        col3.setStyleAttribute("paddingRight", "10px");
        layout = new FormLayout();

        layout.setLabelAlign(LabelAlign.TOP);
        col3.setLayout(layout);

        LayoutContainer col4 = new LayoutContainer();
        // col4.setStyleAttribute("paddingRight", "10px");
        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        col4.setLayout(layout);

        fieldSet.add(col1, new ColumnData(.25));
        fieldSet.add(col2, new ColumnData(.25));
        fieldSet.add(col3, new ColumnData(.25));
        fieldSet.add(col4, new ColumnData(.25));

        FormData formData = new FormData("100%");
        col1.add(authorField, formData);
        col2.add(editorField, formData);
        col3.add(sourceField, formData);
        col4.add(statusField, formData);

        col1.add(rankField, formData);
        col2.add(gradeField, formData);
        col3.add(publishDateField, formData);
        col4.add(expiredDateField, formData);

        col1.add(allowCommentField, formData);
        col2.add(clicksField, formData);
        col3.add(buzzUpField, formData);
        col4.add(buzzDownField, formData);

        return fieldSet;
    }

    private FormPanel createFormPanel() {

        formPanel = new FormPanel();
        formPanel.setLabelWidth(40);
        formPanel.setLabelAlign(LabelAlign.TOP);
        formPanel.setHeading(messages.editArticle());
        formPanel.setScrollMode(Scroll.AUTOY);
        formPanel.setFrame(true);

        titleField = new TextField<String>();
        titleField.setName("title");
        titleField.setFieldLabel(messages.title());
        titleField.setAllowBlank(false);
        titleField.setMaxLength(constants.maxTitleLength());
        titleField.setMessageTarget("tooltip");
        formPanel.add(titleField, new FormData("95%"));

        summaryField = new TextArea();
        summaryField.setHeight(40);
        summaryField.setName("summary");
        summaryField.setFieldLabel(messages.summary());
        summaryField.setMaxLength(constants.maxStringLength());
        formPanel.add(summaryField, new FormData("95%"));

        FieldSet info = createInfoFieldSet();
        formPanel.add(info, new FormData("95%"));

        FieldSet props = createPropertiesFieldSet();
        formPanel.add(props, new FormData("95%"));

        contentField = new HtmlEditor();
        contentField.setName("content");
        contentField.setFieldLabel(messages.content());
        contentField.setHeight(400);
        formPanel.add(contentField, new FormData("95%"));

        formBinding = new FormBinding(formPanel);

        formBinding.addFieldBinding(statusField.getFieldBinding());
        formBinding.addFieldBinding(allowCommentField.getFieldBinding());
        formBinding.autoBind();
        
        submitButton = new Button(messages.submit());
        cancelButton = new Button(messages.cancel());
        formPanel.setButtonAlign(HorizontalAlignment.CENTER);
        formPanel.addButton(submitButton);
        formPanel.addButton(cancelButton);
        FormButtonBinding binding = new FormButtonBinding(formPanel);
        binding.addButton(submitButton);

        return formPanel;

    }

    @Override
    public Widget asWidget() {
        return this;
    }

    public FormPanel getFormPanel() {

        return formPanel;
    }

    @Override
    public Button getCancelButton() {
        return cancelButton;
    }

    @Override
    public Button getSubmitButton() {
        return submitButton;
    }

    @Override
    public FormBinding getFormBinding() {
        return formBinding;
    }

    @Override
    public HtmlEditor getHtmlEditor() {
        return contentField;
    }

    @Override
    public void addWidget(Widget widget) {

    }

    @Override
    public void removeWidget(Widget widget) {

    }

    @Override
    public void showWidget(Widget widget) {

    }

}
